package com.marchevskiy.illiniScholar;

import com.marchevskiy.illiniScholar.ScholarApp;
import com.marchevskiy.illiniScholar.util.Credentials;
import com.marchevskiy.illiniScholar.util.Logger;
import com.marchevskiy.illiniScholar.util.Session;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class IlliniScholarActivity extends Activity {
    Credentials mCred;
    ScholarApp mApp;
    Session mSess;
    Logger l = new Logger("ScholarActivity-Debug");
    
    enum MODE {
    	LOGIN,
    	DEFAULT
    }
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        switch(init()) {
        case LOGIN:
        	mApp = new ScholarApp();
        	setContentView(mApp.getLayout());
        	prepareLoginUI();
        	break;
        case DEFAULT:
        	mApp = new ScholarApp(mCred);
        	setContentView(mApp.getLayout());
        	break;
        default:
        	break;
        }       
        
    }
    
    public MODE init() {
    	return MODE.LOGIN;
    }
    
    private void prepareLoginUI() {
    	log("preparing login ui");
    	
    	((Button)findViewById(R.id.login_button)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getCredentials();
				login();
			}
    		
    	});
    }
    
    private void login() {
    	mSess = new Session(mCred);
    	if (mSess.start()) {
    		
    	} else {
    		
    	}
    }
    
    private void getCredentials() {
    	mCred = new Credentials(((TextView)findViewById(R.id.username)).getText().toString(), ((TextView)findViewById(R.id.password)).getText().toString());
    }
    
    private void log(String s) {
    	l.log(s);
    }
}